const fs = require("fs")
const hjson = require("./hjson.min.js");

function getSetting(path) {
	let configPath = path + "/project.config.json";
	let setting = {};

	try {
		if (fs.existsSync(configPath)) {
			let readFile = fs.readFileSync(configPath, 'utf8');
			let json = hjson.parse(readFile);
			let jsonData;
			if (json && json.setting) {
				jsonData = json.setting;
			}
			if (jsonData) {
				let es6 = jsonData.es6;
				if (es6 !== undefined && typeof(es6) == "boolean") {
					setting.es6 = es6;
				}
				let es7 = jsonData.es7;
				if (es7 !== undefined && typeof(es7) == "boolean") {
					setting.es7 = es7;
				}
				// let minifyJS = jsonData.minifyJS;
				// if (minifyJS !== undefined && typeof(minifyJS) == "boolean") {
				// 	setting.minifyJS = minifyJS;
				// }
				// let minifyWXML = jsonData.minifyWXML;
				// if (minifyWXML !== undefined && typeof(minifyWXML) == "boolean") {
				// 	setting.minifyWXML = minifyWXML;
				// }

				let minifyWXSS = jsonData.minifyWXSS;
				if (minifyWXSS !== undefined && typeof(minifyWXSS) == "boolean") {
					setting.minifyWXSS = minifyWXSS;
				}
				let minified = jsonData.minified;
				if (minified !== undefined && typeof(minified) == "boolean") {
					setting.minify = minified;
				}
				let uglifyFileName = jsonData.uglifyFileName;
				if (uglifyFileName !== undefined && typeof(uglifyFileName) == "boolean") {
					setting.codeProtect = uglifyFileName;
				}
				let postcss = jsonData.postcss;
				if (postcss !== undefined && typeof(postcss) == "boolean") {
					setting.autoPrefixWXSS = postcss;
				}

			}
		}
	} catch (e) {
		console.error("e:", e)
	}

	return setting;
}

module.exports = {
	setting: getSetting
}
